﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using ZeldoEngine;
using ZeldoEngineObjects.Maps;
using ZeldoEngineObjects;

namespace MapEditor.Windows.DataBase
{
    public partial class BaseDeDonneesActions
    {
        public List<Chipset> chipsets;
        public Chipset currentChipset;

        public void ChargerChipsets()
        {
            if (this.chipsets == null)
            {
                this.chipsets = AppParams.GetIns().Project.Ressources.Chipsets.Chipsets;
            }

            this.windows.listBoxChipsets.Items.Clear();

            foreach (var chipset in this.chipsets)
            {
                this.windows.listBoxChipsets.Items.Add(chipset.Nom);
            }

            this.windows.listBoxChipsets.SelectedIndex = 0;

            this.ChargerChipset(this.windows.listBoxChipsets.SelectedItem.ToString());
        }

        public void RafraichirListChipsets()
        {
            foreach (var chipset in this.chipsets)
            {
                this.windows.listBoxChipsets.Items.Clear();
                this.windows.listBoxChipsets.Items.Add(chipset.Nom);
            }
        }

        public void RafraichirChipset()
        {
            this.ChargerChipset(this.currentChipset.Nom);
        }

        public void ChargerChipset(string chipsetName)
        {
            if (currentChipset == null)
            {
                this.currentChipset = AppParams.GetIns().Project.Ressources.Chipsets.LireChipset(chipsetName);
            }

            if (currentChipset != null)
            {
                this.windows.textBoxChipsetLabel.Text = currentChipset.Nom;
                this.windows.textBoxTileset.Text = currentChipset.NomFichier;
                this.windows.textBoxAutotile1.Text = currentChipset.AutoTile1;
                this.windows.textBoxAutotile2.Text = currentChipset.AutoTile2;
                this.windows.textBoxAutotile3.Text = currentChipset.AutoTile3;
                this.windows.textBoxAutotile4.Text = currentChipset.AutoTile4;
                this.windows.textBoxAutotile5.Text = currentChipset.AutoTile5;
                this.windows.textBoxAutotile6.Text = currentChipset.AutoTile6;
                this.windows.textBoxAutotile7.Text = currentChipset.AutoTile7;
            }

            this.ChargerTileset(currentChipset);
        }

        public void ChargerTileset(Chipset chipset)
        {
            var images = new List<Bitmap>();
            Bitmap finalChipset = null;

            int width = EngineConsts.CHIPSET_WIDTH;

            images.Add(new Bitmap(@"Content\Autotiles\" + chipset.AutoTile1 + ".png"));
            images.Add(new Bitmap(@"Content\Autotiles\" + chipset.AutoTile2 + ".png"));
            images.Add(new Bitmap(@"Content\Autotiles\" + chipset.AutoTile3 + ".png"));
            images.Add(new Bitmap(@"Content\Autotiles\" + chipset.AutoTile4 + ".png"));
            images.Add(new Bitmap(@"Content\Autotiles\" + chipset.AutoTile5 + ".png"));
            images.Add(new Bitmap(@"Content\Autotiles\" + chipset.AutoTile6 + ".png"));
            images.Add(new Bitmap(@"Content\Autotiles\" + chipset.AutoTile7 + ".png"));

            var tileset = new Bitmap(@"Content\Chipset\" + chipset.NomFichier + ".png");

            finalChipset = new Bitmap(width, tileset.Height + EngineConsts.TILE_SIZE);

            using (Graphics g = Graphics.FromImage(finalChipset))
            {
                g.Clear(Color.Black);

                int offset = EngineConsts.TILE_SIZE;
                foreach (var image in images)
                {
                    g.DrawImage(image, new Rectangle(offset, 0, EngineConsts.TILE_SIZE, EngineConsts.TILE_SIZE), new Rectangle(0, 0, EngineConsts.TILE_SIZE, EngineConsts.TILE_SIZE), GraphicsUnit.Pixel);
                    offset += EngineConsts.TILE_SIZE;
                }

                g.DrawImage(tileset, new Rectangle(0, EngineConsts.TILE_SIZE, tileset.Width, tileset.Height));
            }

            this.windows.pictureBoxTileset.Image = finalChipset;
        }

        public void EnregistrerChipsets()
        {
            AppParams.GetIns().Project.Ressources.Chipsets.EnregistrerChipsets(this.chipsets);
        }
    }
}
